﻿@using NewLife;
@using System.ComponentModel;
@using System.Reflection;
@using NewLife.Reflection;
@{
    // 强制设置Layout，因为最终项目可能给前端设置Layout，会覆盖影响魔方内部页面
    var theme = NewLife.Cube.CubeSetting.Current.Theme;
    if (String.IsNullOrEmpty(theme)) theme = "ACE";

    Layout = "~/Views/" + theme + "/_Layout.cshtml";

    var set = ViewBag.PageSetting as PageSetting ?? PageSetting.Global;

    var obj = (Object)Model;
    var list = ViewBag.Fields as IList<DataField>;
    var dic = list
        .GroupBy(e => e.Category + "")
        .ToDictionary(e => e.Key.IsNullOrEmpty() ? "默认" : e.Key, e => e.ToList());
}
@if (!set.NavView.IsNullOrEmpty())
{
    <div class="row">
        @await Html.PartialAsync(set.NavView)
    </div>
}
<div class="row">
    <div class="col-md-12">
        <div class="form-horizontal">
            @using (Html.BeginForm("Update", null))
            {
                var i = 0;
                var j = 0;
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)
                @if (dic.Count > 1)
                {
                    <ul class="nav nav-tabs" role="tablist">
                        @foreach (var item in dic)
                        {
                            <li class="@(i==0?"active":"")"><a href="@("#item"+i)" data-toggle="tab">@item.Key</a></li>
                            i++;
                        }
                    </ul>
                    <div class="tab-content">
                        @foreach (var group in dic)
                        {
                            <div class="tab-pane fade in @(j==0?"active":"")" id="@("item"+j)">
                                <div class="row">
                                    @foreach (var pi in group.Value)
                                    {
                                        var name = pi.Name;
                                        var val = obj.GetValue(name);
                                        var dis = pi.DisplayName;
                                        var des = pi.Description;
                                        var dataSource = pi.DataSource;

                                        <div class="form-group col-sm-12 col-md-12">
                                            <label class="control-label col-xs-2 col-md-3 no-padding-left" for="@name">
                                                @(
                                                    dis
                                                    )
                                            </label>
                                            <div class="input-group col-xs-10 col-md-4">
                                                @if (dataSource != null)
                                                {
                                                    @await Html.PartialAsync("_Form_DropDownList", new ListBoxModel(name, dataSource(group.Value), val))
                                                }
                                                else
                                                {
                                                    @await Html.PartialAsync("_Form_Editor", new ItemModel(name, val, pi.Type))
                                                }
                                                @Html.ValidationMessage(name)
                                            </div>
                                            <span class="hidden-xs col-md-5">&nbsp; @des</span>
                                        </div>
                                    }
                                </div>
                            </div>
                            j++;
                        }
                    </div>
                }
                else
                {
                    foreach (var item in dic)
                    {
                        if (dic.Count > 1)
                        {
                            <div class="form-group col-sm-12 col-md-12 col-xd-12">
                                <label class="control-label col-xs-2 col-md-3 no-padding-left">
                                    <h2>@item.Key</h2>
                                </label>
                            </div>
                        }
                        foreach (var pi in item.Value)
                        {
                            var name = pi.Name;
                            var val = obj.GetValue(name);
                            var dis = pi.DisplayName;
                            var des = pi.Description;
                            var dataSource = pi.DataSource;

                            <div class="form-group col-sm-12 col-md-12">
                                <label class="control-label col-xs-2 col-md-3 no-padding-left" for="@name">@(dis)</label>
                                <div class="input-group col-xs-10 col-md-4">
                                    @if (dataSource != null)
                                    {
                                        @await Html.PartialAsync("_Form_DropDownList", new ListBoxModel(name, dataSource(item.Value), val))
                                    }
                                    else
                                    {
                                        @await Html.PartialAsync("_Form_Editor", new ItemModel(name, val, pi.Type))
                                    }
                                    @Html.ValidationMessage(name)
                                </div>
                                <span class="hidden-xs col-md-5">&nbsp; @des</span>
                            </div>
                        }
                    }
                }
                if (this.Has(PermissionFlags.Update))
                {
                    <div class="clearfix form-actions col-sm-12 col-md-12">
                        <label class="control-label col-xs-4 col-sm-5 col-md-5"></label>
                        <button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-save"></i><strong>保存</strong></button>
                        <button type="button" class="btn btn-danger" onclick="history.go(-1);"><i class="glyphicon glyphicon-remove"></i><strong>取消</strong></button>
                    </div>
                }
            }
        </div>
    </div>
</div>